package com.ruoyi.project.agv.controller;

import com.google.gson.Gson;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.project.agv.domain.AgvCallback;
import com.ruoyi.project.agv.domain.AGVResult;
import com.ruoyi.project.agv.service.IAgvCallbackService;
import com.ruoyi.project.mydo.mydojob.domain.MydoJob;
import com.ruoyi.project.mydo.mydojob.service.IMydoJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * AGV海康任务接收通知接口
 */
@RequestMapping("/agv/agvCallbackService")
@RestController
public class FlsAgvController {

    @Autowired
    private IAgvCallbackService iAgvCallbackService;

    @Autowired
    private IMydoJobService mydoJobService;


    /**
     * AGV海康任务接收通知接口
     *
     * @param jsonParam
     * @return
     */
    @Log(title = "海康回传")
    @RequestMapping("/agvCallback")
    public String agvCallback(@RequestBody String jsonParam) {
        Gson gson = new Gson();

        AgvCallback agvCallback = gson.fromJson(jsonParam.trim(), AgvCallback.class);

        System.out.println("AGV任务开始/结束接口：" + agvCallback);
        AGVResult r = new AGVResult();

        AgvCallback a1 = iAgvCallbackService.selectAgvCallbackById(agvCallback.getReqCode());

        if (a1 != null) {
            iAgvCallbackService.updateAgvCallback(agvCallback);
        } else {
            iAgvCallbackService.insertAgvCallback(agvCallback);
        }

        /**
         *
         *  表示入出库结束，调用接口，生成库存或删除库存
         */
        if ("end".equals(agvCallback.getMethod())) {
            ////查询作业
            //String taskNo = agvCallback.getTaskCode().substring(8);
            //System.out.println("作业任务号：" + taskNo);
            //MydoJob mydoJob = new MydoJob();
            //mydoJob.setTaskNo(taskNo);
            //mydoJob.setStatus("3");
            //List<MydoJob> mydoJobs = mydoJobService.selectMydoJobList(mydoJob);
            //if (mydoJobs.size() > 0) {
            //    //ctu任务处理
            //    System.out.println("完成作业" + mydoJobs.get(0));
            //    Map<String, String> map = iAgvCallbackService.impJob(taskNo);
            //    //获取返回结果
            //    String result = map.get("v_result");
            //    //2扫码重复，9存储过程异常，10成功
            //    if (!"".equals(result) && result != null && "10".equals(result)) {
            //        //返回
            //        r.setCode("0");
            //        r.setMessage("成功");
            //        r.setReqCode(agvCallback.getReqCode());
            //    }
            //}
            // 通过reqCode读到小车状态为start的agv执行队列
            Map<String, String> map = iAgvCallbackService.impAgvEnd(agvCallback.getTaskCode());
            String result = map.get("v_result");
            String resultMsg = map.get("v_result_msg");
            r.setCode(result);
            r.setMessage(resultMsg);
            r.setReqCode(agvCallback.getReqCode());

        } else if ("start".equals(agvCallback.getMethod())) {
            // 通过reqCode读到小车状态为start的agv执行队列，将状态改为1 （已下发）
            Map<String, String> map = iAgvCallbackService.impAgvStart(agvCallback.getTaskCode());
            String result = map.get("v_result");
            String resultMsg = map.get("v_result_msg");
            r.setCode(result);
            r.setMessage(resultMsg);
            r.setReqCode(agvCallback.getReqCode());
        } else if ("outbin".equals(agvCallback.getMethod())) {
            // 通过reqCode读到小车状态为start的agv执行队列
            Map<String, String> map = iAgvCallbackService.impAgvOutBin(agvCallback.getTaskCode());
            String result = map.get("v_result");
            String resultMsg = map.get("v_result_msg");
            r.setCode(result);
            r.setMessage(resultMsg);
            r.setReqCode(agvCallback.getReqCode());
        } else if ("apply".equals(agvCallback.getMethod())) {
            // 通过reqCode读到小车状态为start的agv执行队列
            Map<String, String> map = iAgvCallbackService.impAgvApply(agvCallback.getTaskCode());
            String result = map.get("v_result");
            String resultMsg = map.get("v_result_msg");
            r.setCode(result);
            r.setMessage(resultMsg);
            r.setReqCode(agvCallback.getReqCode());
        }
        String jsonString = gson.toJson(r);
        return jsonString;
    }


}
